using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CompanyX.Insurance.La.ServiceContracts;
using CompanyX.Insurance.La.DataContracts;
using CompanyX.Insurance.La.FaultContracts;

namespace CompanyX.Insurance.La.ServiceProviders
{
    /// <summary>
    /// This is a sample implementation.  Provider for NewBusiness server.  
    /// </summary>
    public class NewBusinessBL
    {
        public static TXLifeResponse SubmitNewBusinsssApplication(TXLifeRequest rq)
        {
            TXLifeResponse rs = new TXLifeResponse();

            if (rq.TransType.tc != (int)OLI_LU_TRANS_TYPE_CODES_TC._103)
            {
                rs.TransResult.ResultCode.tc = (int) RESULT_CODES_TC._5;
                ResultInfo info = new ResultInfo();
                info.ResultInfoCode.tc = (int) RESULT_INFO_CODES_TC._100;
                info.ResultInfoCode.Value = "Transaction not supported by this service";
                rs.TransResult.ResultInfo.Add(info);
                return rs;
            }

            // Since we got here, we have a good transaction type. Unpack the data that we
            // need to pass to the business logic layer, and make sure that we have the right
            // types and numbers

            // First, fetch the data that we were sent by the client.
            // There should be just one party and one holding in the OLifE. 
            // For the purposes of this example, we will ignore the error case of multiple
            // parties/holdings.

            Party party = rq.OLifE.Party.Count > 0 ? rq.OLifE.Party[0] : null;
            if (party == null)
            {
                rs.TransResult.ResultCode.tc = (int)RESULT_CODES_TC._5;
                ResultInfo info = new ResultInfo();
                info.ResultInfoCode.tc = (int)RESULT_INFO_CODES_TC._200;
                info.ResultInfoCode.Value = "No Party Supplied";
                rs.TransResult.ResultInfo.Add(info);       
                return rs;
            }

            // pass party to fullfullment server

            Holding holding = new Holding();
            holding.HoldingTypeCode.tc = (int)OLI_LU_HOLDTYPE_TC._1;
            holding.HoldingTypeCode.Value = "Policy";
            holding.Policy.PolNumber = Guid.NewGuid().ToString();
            holding.Policy.PolicyStatus.tc = (int)OLI_LU_POLSTAT_TC._1;
            holding.Policy.PolicyStatus.Value = "Application Recieved";
            rs.OLifE.Holding.Add(holding);

            rs.TransResult.ResultCode.tc = (int)RESULT_CODES_TC._2;
            rs.TransResult.ResultCode.Value = "Success with Info";
            ResultInfo info1 = new ResultInfo();
            info1.ResultInfoCode.tc = (int)RESULT_INFO_CODES_TC._4000;
            info1.ResultInfoCode.Value = "Agent Can Sell";
            rs.TransResult.ResultInfo.Add(info1);

            return rs;
        }

        public static TXLifeResponse GetGeneralRequirementStatusAndResult(TXLifeRequest rq)
        {
            TXLifeResponse rs = new TXLifeResponse();

            if (rq.TransType.tc != (int)OLI_LU_TRANS_TYPE_CODES_TC._122)
            {
                rs.TransResult.ResultCode.tc = (int)RESULT_CODES_TC._5;
                ResultInfo info = new ResultInfo();
                info.ResultInfoCode.tc = (int)RESULT_INFO_CODES_TC._100;
                info.ResultInfoCode.Value = "Transaction not supported by this service";
                rs.TransResult.ResultInfo.Add(info);
                return rs;
            }

            // Since we got here, we have a good transaction type. Unpack the data that we
            // need to pass to the business logic layer, and make sure that we have the right
            // types and numbers

            // First, fetch the data that we were sent by the client.
            // There should be just one party and one holding in the OLifE. 
            // For the purposes of this example, we will ignore the error case of multiple
            // parties/holdings.

            Holding holding = rq.OLifE.Holding.Count > 0 ? rq.OLifE.Holding[0] : null;

            if (holding == null)
            {
                rs.TransResult.ResultCode.tc = (int)RESULT_CODES_TC._5;
                ResultInfo info = new ResultInfo();
                info.ResultInfoCode.tc = (int)RESULT_INFO_CODES_TC._200;
                info.ResultInfoCode.Value = "No Holding Supplied";
                rs.TransResult.ResultInfo.Add(info);
                return rs;
            }

            // Since we got here, we know that we have the data objects that we need to 
            // answer the question. Delegate to a business logic object, which we can
            // replace with a mock for testing/development

            // pass party to fullfullment server

            holding.Policy.PolicyStatus.Value = "Application Approved";
            rs.OLifE.Holding.Add(holding);

            rs.TransResult.ResultCode.tc = (int)RESULT_CODES_TC._2;
            rs.TransResult.ResultCode.Value = "Success with Info";
            ResultInfo info1 = new ResultInfo();
            info1.ResultInfoCode.tc = (int)RESULT_INFO_CODES_TC._4000;
            info1.ResultInfoCode.Value = "Agent Can Sell";
            rs.TransResult.ResultInfo.Add(info1);

            return rs;
        }
    }
}